<template>
  <div class="mod-user">
    <el-form :inline="true" :model="formData" label-width="100px" label-position="right">
      <el-form-item label="合伙人名称" v-if="false">
        <el-input @change="searchChange" placeholder="合伙人名称" v-model="formData.partnerName" size="small"
          clearable></el-input>
      </el-form-item>
      <el-form-item label="合伙人手机">
        <el-input @change="searchChange" placeholder="手机号" v-model="formData.partnerMobile" size="small"
          clearable></el-input>
      </el-form-item>
      <el-form-item label="账单时间">
        <el-input @change="searchChange" placeholder="账单时间(示例:20240231)" v-model="formData.billTime" size="small"
          clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click.stop="searchChange">搜索</el-button>
      </el-form-item>
    </el-form>
    <avue-crud ref="crud" :page="page" :data="dataList" :option="tableOption" @size-change="sizeChange"
      @current-change="currentChange">
      <template slot-scope="scope" slot="isPay">
        <el-tag v-if="scope.row.isPay === 0" size="small" type="danger">未支付</el-tag>
        <el-tag v-else size="small">已支付</el-tag>
      </template>
      <template slot-scope="scope" slot="isConfirm">
        <el-tag v-if="scope.row.isConfirm === 0" size="small" type="danger">未审核</el-tag>
        <el-tag v-if="scope.row.isConfirm === 1" size="small">已审核</el-tag>
        <el-tag v-else-if="scope.row.isConfirm === 2" size="small">已拒绝</el-tag>
      </template>
      <template class="billContent" slot-scope="scope" slot="billDesc">
        <div style="display: flex; flex-direction: column; align-items: center;">
          <div >账单金额：<span>{{ scope.row.money}}元</span></div>
          <div >账单描述：<span>{{ scope.row.payDetailDesc}}</span></div>
        </div>
      </template>
      <template class="billContent" slot-scope="scope" slot="userInfo">
        <div style="display: flex; flex-direction: column; align-items: center;">
          <div >付款人：<span>{{ scope.row.payer}}</span></div>
          <div >收款人：<span>{{ scope.row.receiver}}</span></div>
        </div>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-row v-if="scope.row.isConfirm === 0">
          <el-button type="primary"
                     size="small"
                     @click="audit(scope.row.id,1)">审核通过</el-button>
          <el-button type="danger"
                     size="small"
                     @click="audit(scope.row.id,2)">不通过</el-button>
        </el-row>
        <el-button type="primary" size="small" @click.stop="payBill(scope.row.id)" v-if="scope.row.isPay === 0 && scope.row.isConfirm === 1">支付</el-button>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { tableOption } from '@/crud/partner/billPayList'
export default {
  data() {
    return {
      dataList: [],
      dataListLoading: false,
      tableOption: tableOption,
      formData: {
        partnerName: '',
        partnerMobile: ''
      },
      partnerDeviceBillParamVo: {},
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      options: [],
      props: {
        multiple: false,
        label: 'name',
        value: 'id',
        children: 'childs'
      },
    }
  },

  mounted() {
    this.getDataList()
  },

  methods: {
    // 获取数据列表
    getDataList(page, params) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/v1/partner/bill/pay/detail/list'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            params
          )
        )
      }).then(({ data }) => {
        this.dataList = data.data.records
        this.page.total = data.data.total
        this.dataListLoading = false
      })
    },

    // 条件查询
    searchChange() {
      this.getDataList(this.page, this.formData)
    },

    audit: function (id, status)  {
      console.log("id是", id)
      this.$confirm(`确定进行[${status === 1 ? '审核通过' : '拒绝'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/v1/partner/bill/pay/detail/confirm'),
          method: 'post',
          data: this.$http.adornData({
            id: id,
            status: status
          })
        }).then(({ data }) => {
          this.dataListLoading = false
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      }).catch(() => { })
    },

    payBill(id) {
      this.$confirm(`确定进行[支付]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/v1/partner/bill/pay/detail/pay'),
          method: 'post',
          data: this.$http.adornData(
            Object.assign(
              {
                id: id
              }
            )
          )
        }).then(({ data }) => {
          if (data.code === 0) {
            this.$message({
              message: '扣款成功',
              type: 'success'
            });
          } else {
            this.$message({
              message: '扣款失败',
              type: 'error'
            }).catch(error => {
              console.error(error);
              this.$message({
                message: '请求失败',
                type: 'error'
              });
            });;
          }
          this.getDataList(this.page, this.formData)
        })
      }).catch(() => { })
    },
    sizeChange(val) {
      this.page.pageSize = val
      this.getDataList(this.page, this.formData)
    },
    currentChange(val) {
      this.page.currentPage = val
      this.getDataList(this.page, this.formData)
    }
  }
}
</script>

<style lang="scss">
/*.cell{*/
/*text-align: left;*/
/*}*/
.el-input {
  width: 200px;
}
</style>
